/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#include "mod_visual_script.h"
#include "../../common/debug.h"

namespace sleepyface
{
    namespace arch
    {
        namespace modules
        {
            VisualScript::VisualScript() : ArchModule()
            {
                setName("visual-script");
            }

            ModuleValue VisualScript::get(string k0, string k1, Architecture &a)
            {
                if (hints.find(k0) != hints.end() && k1.compare("x") == 0)
                {
                    Z_DEBUG(verbose, "retrieving x-offset of hint " << k0);
                    return ModuleValue(ModuleValue::_int, hints[k0].getX0());
                }
                else if (hints.find(k0) != hints.end() && k1.compare("y") == 0)
                {
                    Z_DEBUG(verbose, "retrieving y-offset of hint " << k0);
                    return ModuleValue(ModuleValue::_int, hints[k0].getY0());
                }
                else if (hints.find(k0) != hints.end() && k1.compare("w") == 0)
                {
                    Z_DEBUG(verbose, "retrieving width of hint " << k0);
                    return ModuleValue(ModuleValue::_int, hints[k0].getWidth());
                }
                else if (hints.find(k0) != hints.end() && k1.compare("h") == 0)
                {
                    Z_DEBUG(verbose, "retrieving height of hint " << k0);
                    return ModuleValue(ModuleValue::_int, hints[k0].getHeight());
                }
                else
                {
                    Z_DEBUG(verbose, "unknown hint " << k0 << " or unknown hint attribute " << k1);
                    return ModuleValue();
                }
            }

            ModuleValue VisualScript::set(string k0, string k1, ModuleValue v, Architecture &a)
            {
                if (hints.find(k0) == hints.end())
                {
                    Z_DEBUG(verbose, "creating new hint " << k0);
                    hints[k0] = visual::Region();
                }

                if (k1.compare("x") == 0)
                {
                    Z_DEBUG(verbose, "setting x-offset of hint " << k0);
                    hints[k0].setX0(v.getInt());
                    return v;
                }
                else if (k1.compare("y") == 0)
                {
                    Z_DEBUG(verbose, "setting y-offset of hint " << k0);
                    hints[k0].setY0(v.getInt());
                    return v;
                }
                else if (k1.compare("w") == 0)
                {
                    Z_DEBUG(verbose, "setting width of hint " << k0);
                    hints[k0].setWidth(v.getInt());
                    return v;
                }
                else if (k1.compare("h") == 0)
                {
                    Z_DEBUG(verbose, "setting height of hint " << k0);
                    hints[k0].setHeight(v.getInt());
                    return v;
                }
                else if (k1.compare("aw") == 0)
                {
                    Z_DEBUG(verbose, "setting width of actual " << k0);
                    actuals[k0].setWidth(v.getInt());
                    return v;
                }
                else if (k1.compare("ah") == 0)
                {
                    Z_DEBUG(verbose, "setting height of actual " << k0);
                    actuals[k0].setHeight(v.getInt());
                    return v;
                }
                else
                {
                    Z_DEBUG(verbose, "unknown hint " << k0 << " or unknown hint attribute " << k1);
                    return ModuleValue();
                }
            }

            int VisualScript::signal(ModuleSignal s, Architecture &a)
            {
                if (s.getCode() == signalInitialize)
                {
                    connection = s.getData(0).getString();
					if (connection.empty())
						connection = "visual-server";
                    Z_DEBUG(verbose, "initializing using connection " << connection);

                    if (!a.isLoaded("ipc-client"))
                    {
                        Z_WARNING("visual scripts don't work without the ipc-client module");
                        return 1;
                    }
                    return 0;
                }
                else if (s.getCode() == signalUninitialize)
                {
                    return 0;
                }
                else if (s.getCode() == signalRequestAppSpace || s.getCode() == signalRequestPaneSpace)
                {
                    int *buf;
                    if (s.getCode() == signalRequestAppSpace)
                    {
                        Z_DEBUG(verbose, "requesting app space");
                        buf = (int *)malloc(sizeof(int)*6);
                        buf[0] = -1;
                    }
                    else if (s.getCode() == signalRequestPaneSpace)
                    {
                        Z_DEBUG(verbose, "requesting pane space");
                        buf = (int *)malloc(sizeof(int)*10);
                        buf[0] = -2;
                    }

                    buf[1] = getpid();
                    buf[2] = hints[s.getData(0).getString()].getX0();
                    buf[3] = hints[s.getData(0).getString()].getY0();
                    buf[4] = hints[s.getData(0).getString()].getX1();
                    buf[5] = hints[s.getData(0).getString()].getY1();
                    z_data *d;
                    if (s.getCode() == signalRequestPaneSpace)
                    {
                        buf[6] = actuals[s.getData(0).getString()].getX0();
                        buf[7] = actuals[s.getData(0).getString()].getY0();
                        buf[8] = actuals[s.getData(0).getString()].getX1();
                        buf[9] = actuals[s.getData(0).getString()].getY1();
                        d = z_data_new(Z_TYPEID_NULL, sizeof(int)*10);
                    }
                    else
                    {
                        d = z_data_new(Z_TYPEID_NULL, sizeof(int)*6);
                    }
                    z_data_src_set(d, buf);
                    a.module("ipc-client")->set(connection, "", ModuleValue(ModuleValue::_data, d), a);
                    z_data *r = (z_data *)a.module("ipc-client")->get(connection, "", a).getData();
                    int *rb = (int *)z_data_src_get(r);
                    hints[s.getData(0).getString()].setX0(rb[0]);
                    hints[s.getData(0).getString()].setY0(rb[1]);
                    hints[s.getData(0).getString()].setX1(rb[2]);
                    hints[s.getData(0).getString()].setY1(rb[3]);
                    z_data_delete(r);
                    z_data_delete(d);
                    return 0;
                }
                else
                {
                    return 0;
                }
            }
        }
    }
}
